import { getDictByMultiCode } from '@/api/module/dict'

/**
 * 多个code获取字典数据并根据传入的code返回object(Promise)
 * @param code
 * @returns {Promise<unknown>}
 */
export const getDictByCodesReturnObj = (code = []) => {
  return new Promise((resolve, reject) => {
    getDictByMultiCode({ code: code.join(',') }).then(res => {
      if (res.code === 200) {
        const data = res.data.reduce((obj, item) => {
          item.text = item.label
          // eslint-disable-next-line no-prototype-builtins
          obj.hasOwnProperty(item.code)
            ? obj[item.code].push(item)
            : obj[item.code] = [item]
          return obj
        }, {})
        resolve(data)
      }
      reject()
    })
  })
}
